﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;
using Xacml.Core.Runtime.DataTypes;

namespace Xacml.Core.Runtime.Functions
{
  public class DateAddYearMonthDuration : FunctionBase
  {
    public override string Id
    {
      get
      {
        return "urn:oasis:names:tc:xacml:1.0:function:date-add-yearMonthDuration";
      }
    }

    public override IDataType Returns
    {
      get
      {
        return (IDataType) DataTypeDescriptor.Date;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[2]
        {
          (IDataType) DataTypeDescriptor.DateTime,
          (IDataType) DataTypeDescriptor.YearMonthDuration
        };
      }
    }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      System.DateTime dateTime = this.GetDateArgument(args, 0);
      YearMonthDuration yearMonthDuration = (YearMonthDuration) args[1].GetTypedValue((IDataType) DataTypeDescriptor.YearMonthDuration, 1);
      dateTime = dateTime.AddYears(yearMonthDuration.Years);
      dateTime = dateTime.AddMonths(yearMonthDuration.Months);
      return new EvaluationValue((object) dateTime, (IDataType) DataTypeDescriptor.Date);
    }
  }
}
